define(['jquery', 'bootstrap', 'frontend', 'template', 'form'],
    function ($, undefined, Frontend, Template, Form) {
        $.fn.popover.Constructor.DEFAULTS.sanitize = false;
        var Controller = {
            create: function () {
                // Controller.api.bindevent();
                $('.radio_label input').on('click', function () {
                    var desc = $(this).data('desc');
                    $('.urgency_desc').html(desc);
                });
                Form.api.bindevent($('form[role=form]'), function (data, ret) {
                    setTimeout(function () {
                        location.href = Fast.api.fixurl(ret.url);
                    }, 1500);
                });
            },
            my: function () {
                Controller.api.on_del_workorder('my');
            },
            detail: function () {
                // Controller.api.bindevent();

                // 拉到页尾
                // window.scrollTo(0, $('main.content').height());

                Controller.api.on_del_workorder('detail');
                $("[rel=engineer_popo]").popover({
                    trigger: 'manual',
                    title: this.title,
                    html: true,
                    content: function () {
                        let engineer_id = $(this).data('engineer_id');
                        return $('.engineer_info_' + engineer_id).html();
                    },
                    animation: true
                }).on("mouseenter", function () {
                    var _this = this;
                    $(this).popover("show");
                    $(this).siblings(".popover").on("mouseleave", function () {
                        $(_this).popover('hide');
                    });
                }).on("mouseleave", function () {
                    var _this = this;
                    setTimeout(function () {
                        if (!$(".popover:hover").length) {
                            $(_this).popover("hide")
                        }
                    }, 100);
                });

                $("[rel=userInfo_popo]").popover({
                    trigger: 'manual',
                    title: this.title,
                    html: true,
                    content: function () {
                        return $('.user_info').html();
                    },
                    animation: true
                }).on("mouseenter", function () {
                    var _this = this;
                    $(this).popover("show");
                    $(this).siblings(".popover").on("mouseleave", function () {
                        $(_this).popover('hide');
                    });
                }).on("mouseleave", function () {
                    var _this = this;
                    setTimeout(function () {
                        if (!$(".popover:hover").length) {
                            $(_this).popover("hide")
                        }
                    }, 100);
                });

                $('.urging_workorder').on('click', function () {
                    var order_id = $(this).data('order_id');
                    Fast.api.ajax({
                        url: 'workorder/detail',
                        data: {
                            type: 'urging',
                            id: order_id
                        }
                    }, function (data, ret) {

                    });
                });

                $('.close_workorder').on('click', function () {
                    var order_id = $(this).data('order_id');
                    Fast.api.ajax({
                        url: 'workorder/detail',
                        data: {
                            type: 'close',
                            id: order_id
                        }
                    }, function (data, ret) {
                        setTimeout(function (action) {
                            location.reload()
                        }, 1500);
                    });
                });

                $('.transfer_workorder').on('click', function () {
                    $('.transfer_form').toggle(100);
                });

                $(document).on('click', '.confidential_btn', function () {
                    var confidential_id = $(this).data('confidential_id');
                    Fast.api.ajax({
                        url: '/api/workorder/showConfidential',
                        data: {
                            id: confidential_id
                        }
                    }, function (data, ret) {
                        $('#confidential_' + data.id).html(data.message);
                        return false;
                    });
                });

                $(document).on('click', '.item_meta_text img', function (e) {
                    var img_obj = $(e.target)[0];
                    if (img_obj) {
                        var scrollTop = $(window).scrollTop();
                        layer.photos({
                            photos: {
                                "title": "图片预览",
                                "id": "recordimg",
                                data: [
                                    {
                                        "src": img_obj.src
                                    }
                                ]
                            },
                            end: function () {
                                $(window).scrollTop(scrollTop)
                            },
                            anim: 5
                        });
                    }
                })

                Form.api.bindevent($('.message_form'), function (data, ret) {
                    if (ret.code == 1) {
                        for (let i in data.recordsHtml) {
                            $('#message_items').append(data.recordsHtml[i]);
                        }
                        $('#message_form')[0].reset();
                        $('#message_form .btn-trash').click()
                        Controller.api.bind_message_back(Config.editor_name);
                        return false;
                    }
                });

                Form.api.bindevent($('.transfer_form'), function (data, ret) {
                    setTimeout(function () {
                        location.href = Fast.api.fixurl(ret.url);
                    }, 1500);
                });
            },
            evaluate: function () {
                var num = finalnum = tempnum = 0;
                var stars = $('.workorder_star i');
                for (var i = 1; i <= stars.length; i++) {
                    stars[i - 1].index = i;
                    stars[i - 1].onmouseover = function () {
                        Controller.light_up_star(this.index, finalnum, tempnum);
                    }
                    stars[i - 1].onmouseout = function () {
                        Controller.light_up_star(0, finalnum, tempnum);
                    }
                    stars[i - 1].onclick = function () {
                        tempnum = this.index;
                        $('#row_stars').val(tempnum)
                    }
                }

                Form.api.bindevent($('form[role=form]'), function (data, ret) {
                    setTimeout(function () {
                        location.href = Fast.api.fixurl(ret.url);
                    }, 1500);
                });
            },
            light_up_star: function (num, finalnum, tempnum) {
                var stars = $('.workorder_star i');
                var finalnum = num || tempnum;
                for (var i = 0; i < stars.length; i++) {
                    stars[i].className = i < finalnum ? "fa fa-star text-yellow" : "fa fa-star text-gray";
                }
            },
            manage: function () {

            },
            kbs: function () {
                $('.likes_btn').on('click', function () {
                    Fast.api.ajax({
                        url: 'workorder/kbs',
                        data: {
                            type: 'likes',
                            id: $(this).data('id')
                        }
                    }, function (data, ret) {
                        $('.likes_btn span').html(parseInt($('.likes_btn span').html()) + 1);
                        return false;
                    });
                });
                $('.dislikes_btn').on('click', function () {
                    Fast.api.ajax({
                        url: 'workorder/kbs',
                        data: {
                            type: 'dislikes',
                            id: $(this).data('id')
                        }
                    }, function (data, ret) {
                        $('.dislikes_btn span').html(parseInt($('.dislikes_btn span').html()) + 1);
                        return false;
                    });
                });
            },
            api: {
                bindevent: function () {
                    Form.api.bindevent($('form[role=form]'));
                },
                bind_message_back: function (editor) {
                    if (editor == 'nkeditor') {
                        var child = $('#message_form .ke-edit-iframe');
                        if (child[0]) {
                            let child_window = child[0].contentWindow
                            var content = child_window.document.getElementsByClassName('ke-content')
                            if (content[0]) {
                                content[0].innerHTML = '';
                            }
                        }
                    } else if (editor == 'none') {
                        $('#message_form textarea[role=reply-describe]').val('')
                    } else if (editor == 'summernote') {
                        $('#message_form .note-editable').html('');
                    } else if (editor == 'markdown') {
                        $('#message_form textarea[role=reply-describe]').val('')
                    } else if (editor == 'simditor') {
                        $('#message_form .simditor-body').html('');
                        $('#message_form .simditor-placeholder').show();
                    } else if (editor == 'umeditor') {
                        $('#message_form .editor').html('');
                    } else if (editor == 'tinymce') {
                        var child = $('#message_form .tox-edit-area__iframe');
                        if (child[0]) {
                            let child_window = child[0].contentWindow
                            child_window.document.getElementById('tinymce').innerHTML = '';
                        }
                    } else {
                        location.reload()
                    }
                },
                on_del_workorder: function (action) {
                    $('.del_workorder').on('click', function (e) {
                        var that = this;
                        e.stopPropagation();
                        e.preventDefault();
                        var order_id = $(that).data('order_id');
                        var order_title = $(that).data('order_title');
                        var layer_msg = order_title ? '您确定要删除工单 `' + order_title + '` 吗？' : '您确定要删除此工单吗？';
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(layer_msg,
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                Fast.api.ajax({
                                    url: 'workorder/my',
                                    data: {
                                        order_id: order_id
                                    },
                                    type: 'delete'
                                }, function (data, ret) {
                                    setTimeout(function (action) {
                                        if (action == 'my') {
                                            location.reload()
                                        } else {
                                            location.href = Fast.api.fixurl('workorder/my');
                                        }
                                    }, 1500, action);
                                });
                                Layer.close(index);
                            }
                        );
                    });
                }
            },
        };
        return Controller;
    });